package model

import (
	"fmt"
	"gowork/1+NService/utils"
	"strconv"
	"strings"
	"time"
)

type NService struct {
	Id int64 `xorm:"pk autoincr"`
	CustomerName string
	CustomerId int64
	RecentOrder string
	RecentFollowResults string
	NotFollowedDay int
	TransactionTimes int
	AssemblySheet int64
	CustomerType int
	AssignServicePersonnelName string
	AssignServicePersonnelId int64
	AssignServiceDate string
	State int
	OrganId string
	CreatedAt time.Time
	UpdatedAt time.Time
 }

type Staff struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	RoleId int64
	RoleName string
	OrganId string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Customer struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type WaiterInfo struct {
	CustomerId int64
	CustomerName string
	WaiterId int64
	WaiterName string
}

func (nService *NService)ImportNServiceInfo(sId int64, wiList []string)(map[string]interface{}, error){

	w := new(utils.Worker)

	var s Staff
	x.Id(sId).Get(&s)

	for _, wi := range wiList[1:len(wiList)]{
		infoList := strings.Split(wi, ",")

		fmt.Println("infoList：", infoList)
		fmt.Println("infoList[0]：", infoList[0])
		fmt.Println("infoList[1]：", infoList[1])

		if infoList[0] == "" || infoList[1] == ""{
			return map[string]interface{}{"message": "客户Id跟客户姓名不能为空","code":400}, nil
		}

		var cu Customer
		x.Id(infoList[0]).Get(&cu)
		if cu.Id == 0 {
			return map[string]interface{}{"message": "客户不存在","code":400}, nil
		}

		var ws Staff
		x.Id(infoList[2]).Get(&ws)
		if ws.Id == 0 && infoList[2] != ""{
			return map[string]interface{}{"message": "顾问不存在","code":400}, nil
		}

		var sws Staff
		//x.Join("INNER", "department", "staff.department_id = department.id").
		//	Where("department.organ_id like ?", organId+"%").
		//	And("staff.id = ?", infoList[2]).
		//	Get(&sws)
		x.Where("organ_id like ?", s.OrganId+"%").And("id = ?", infoList[2]).Get((&sws))
		if sws.Id == 0 && infoList[2] != ""{
			return map[string]interface{}{"message": "非本部门顾问","code":400}, nil
		}

		var ns NService
		
		//x.Join("INNER", "staff", "staff.id = n_service.assign_service_personnel_id").
		//	Where("staff.organ_id like ?", s.OrganId[0:5]+"%").
		//	And("n_service.customer_id = ?", infoList[0]).
		//	Get(&ns)
		x.Where("organ_id like ?", s.OrganId[0:6]+"%").
			And("customer_id = ?", infoList[0]).
			Get(&ns)

		fmt.Println("ns.Id：", ns.Id)

		if ns.Id != 0{
			if ns.State == 1 {
				if infoList[2] != "" {
					ns.State = 2
					assignServicePersonnelId, err := strconv.ParseInt(infoList[2], 10, 64)
					if err != nil {
						return map[string]interface{}{"message": "解析错误", "code": 500}, nil
					}
					ns.AssignServicePersonnelId = assignServicePersonnelId
					ns.AssignServicePersonnelName = infoList[3]
					ns.UpdatedAt = time.Now()
					x.Id(ns.Id).Update(&ns)
				}
			}else {
				if infoList[2] == "" {
					ns.State = 1
					ns.AssignServicePersonnelId = 0
					ns.AssignServicePersonnelName = ""
					ns.UpdatedAt = time.Now()
					x.Id(ns.Id).Cols("assign_service_personnel_id", "assign_service_personnel_name", "state", "updated_at").Update(&ns)
				}else {
					assignServicePersonnelId, err := strconv.ParseInt(infoList[2], 10, 64)
					if err != nil {
						return map[string]interface{}{"message": "解析错误", "code": 500}, nil
					}
					ns.AssignServicePersonnelId = assignServicePersonnelId
					ns.AssignServicePersonnelName = infoList[3]
					ns.UpdatedAt = time.Now()
					x.Id(ns.Id).Update(&ns)
				}
			}
		}else {
			customerId, err := strconv.ParseInt(infoList[0],10, 64)
			if err != nil{
				return map[string]interface{}{"message": "解析错误","code":500}, nil
			}
			ns.CustomerId = customerId
			ns.CustomerName = infoList[1]
			if infoList[2] == ""{
				ns.Id = w.GetId()
				ns.State = 1
				ns.AssignServicePersonnelId = 0
				ns.AssignServicePersonnelName = ""
				ns.OrganId = s.OrganId
				ns.CreatedAt = time.Now()
				ns.UpdatedAt = time.Now()
				x.Insert(&ns)
			}else {
				ns.State = 2
				ns.Id = w.GetId()
				assignServicePersonnelId, err := strconv.ParseInt(infoList[2],10, 64)
				if err != nil{
					return map[string]interface{}{"message": "解析错误","code":500}, nil
				}
				ns.AssignServicePersonnelId = assignServicePersonnelId
				ns.AssignServicePersonnelName = infoList[3]
				ns.OrganId = s.OrganId
				ns.CreatedAt = time.Now()
				ns.UpdatedAt = time.Now()
				x.Insert(&ns)
			}
		}
	}

	return map[string]interface{}{"message": "成功","code":200}, nil
}

func (nService *NService)Distribution(nsId int64, sId int64, sName string, lsId int64)(map[string]interface{}, error){

	var s Staff
	x.Id(lsId).Get(&s)

	var ws Staff
	x.Id(sId).Get(&ws)
	if ws.Id == 0{
		return map[string]interface{}{"message": "顾问不存在","code":400}, nil
	}

	var sws Staff
	x.Where("organ_id like ?", s.OrganId+"%").And("id = ?", sId).Get(&sws)
	if sws.Id == 0{
		return map[string]interface{}{"message": "非本部门顾问","code":400}, nil
	}

	var ns NService
	ns.State = 2
	ns.AssignServicePersonnelId = sId
	ns.AssignServicePersonnelName = sName
	x.Id(nsId).Update(&ns)

	return map[string]interface{}{"message": "成功","code":200}, nil
}

func (nService *NService)QueryNServiceInfo(sId int64)([]*NService, error){

	var s Staff
	x.Id(sId).Get(&s)

	var nsList []*NService

	fmt.Println("s.OrganId[0:6]：", s.OrganId[0:6])
	if s.OrganId[0:6] == "022060" || s.OrganId[0:6] == "022070" || s.OrganId[0:6] == "022080" || s.OrganId[0:6] == "022090" || s.OrganId[0:6] == "022100"{
		if s.RoleId == 316282793803907072{
			//x.Join("INNER", "staff", "staff.id = n_service.assign_service_personnel_id").
			//	Where("staff.organ_id like ?", s.OrganId+"%").
			//	Find(&nsList)

			//x.Join("INNER", "department", "staff.department_id = department.id").
			//Where("department.organ_id like ?", organId+"%").
			//	Find(&nsList)

			x.Where("organ_id like ?", s.OrganId+"%").
				Find(&nsList)

			//var notDnsList []*NService
			//x.Where("state = ?", 1).Find(&notDnsList)

			//nsList = append(nsList, notDnsList...)

		}else if s.RoleId == 316282793803907073{
			x.Where("assign_service_personnel_id = ?", sId).
				Find(&nsList)
		}
	}

	fmt.Println("nsList：", nsList)
	return nsList, nil
}
